/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.system.rest;

import cn.hutool.core.collection.CollectionUtil;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.GodownEntry;
import me.zhengjie.modules.system.domain.GodownEntryDetail;
import me.zhengjie.modules.system.repository.GodownEntryRepository;
import me.zhengjie.modules.system.service.GodownEntryDetailService;
import me.zhengjie.modules.system.service.dto.GodownEntryDetailQueryCriteria;
import me.zhengjie.modules.system.service.dto.PurchaseDataDto;
import me.zhengjie.modules.system.service.dto.PurchaseDataQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import me.zhengjie.utils.PageResult;
import me.zhengjie.modules.system.service.dto.GodownEntryDetailDto;

/**
 * @author 李忠臻
 * @website https://eladmin.vip
 * @date 2024-02-24
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "入库单明细管理")
@RequestMapping("/api/godownEntryDetail")
public class GodownEntryDetailController {

    private final GodownEntryDetailService godownEntryDetailService;
    private final GodownEntryRepository godownEntryRepository;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('godownEntryDetail:list')")
    public void exportGodownEntryDetail(HttpServletResponse response, GodownEntryDetailQueryCriteria criteria) throws IOException {
        if (CollectionUtil.isEmpty(criteria.getGodownEntryId())) {
            //默认查询状态为审核通过的数据
            List<GodownEntry> godownEntryList = godownEntryRepository.findByStatus(1);
            List<Long> idList = godownEntryList.stream().map(GodownEntry::getId).collect(Collectors.toList());
            criteria.setGodownEntryId(idList);
        }
        godownEntryDetailService.download(godownEntryDetailService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询入库单申请明细")
    @ApiOperation("查询入库单申请明细")
//    @PreAuthorize("@el.check('godownEntryDetail:list')")
    public ResponseEntity<PageResult<GodownEntryDetailDto>> queryGodownEntryDetail(GodownEntryDetailQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(godownEntryDetailService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增入库单申请明细")
    @ApiOperation("新增入库单申请明细")
    @PreAuthorize("@el.check('godownEntryDetail:add')")
    public ResponseEntity<Object> createGodownEntryDetail(@Validated @RequestBody GodownEntryDetail resources) {
        godownEntryDetailService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改入库单申请明细")
    @ApiOperation("修改入库单申请明细")
    @PreAuthorize("@el.check('godownEntryDetail:edit')")
    public ResponseEntity<Object> updateGodownEntryDetail(@Validated @RequestBody GodownEntryDetail resources) {
        godownEntryDetailService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除入库单申请明细l")
    @ApiOperation("删除入库单申请明细")
    @PreAuthorize("@el.check('godownEntryDetail:del')")
    public ResponseEntity<Object> deleteGodownEntryDetail(@RequestBody Long[] ids) {
        godownEntryDetailService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @GetMapping(value = "/list/{id}")
    @Log("入库单申请明细")
    @ApiOperation("入库单申请明细")
//    @PreAuthorize("@el.check('godownEntry:list')")
    public ResponseEntity<Object> listGodownEntryDetail(@PathVariable Long id) {

        return new ResponseEntity<>(godownEntryDetailService.queryByGodownEntryId(id), HttpStatus.OK);
    }

    @PostMapping(value = "/getSupplierSettlmentData")
    @Log("获取供应商结算数据")
    @ApiOperation("获取供应商结算数据")
//    @PreAuthorize("@el.check('purchaseData:list')")
    public ResponseEntity<List<GodownEntryDetailDto>> getSupplierSettlmentData(GodownEntryDetailQueryCriteria criteria) {
        return new ResponseEntity<>(godownEntryDetailService.getSupplierSettlmentData(criteria), HttpStatus.OK);
    }
}